var refreshBalance, refreshDebit;

$('#:employeeId').change(function() {
  return refreshDebit();
});

$('#:debit').change(function() {
  return refreshBalance();
});

$('#:name').change(function() {
  return refreshDebit();
});

refreshDebit = function() {
  var data, employee, name, success;
  employee = $('#:employeeId').val();
  if (employee === '') {
    return;
  }
  name = $('#:name').val();
  if (name === '') {
    return;
  }
  data = {
    'name': name,
    'employeeId': employee,
    'ajax': 'get-quota'
  };
  success = function(json) {
    $('#:debit').val(json.quota);
    refreshBalance();
  };
  $.getJSON(':quotaUrl', data, success);
};

refreshBalance = function() {
  var data, employee, success;
  employee = $('#:employeeId').val();
  if (employee === '') {
    return;
  }
  data = {
    'employeeId': employee,
    'ajax': 'get-balance'
  };
  success = function(json) {
    var debit;
    debit = parseInt($('#:debit').val());
    if (isNaN(debit) || ($('#:name').val() !== ':annualy')) {
      debit = 0;
    }
    $('#:balance').val(json.balance - debit);
  };
  $.getJSON(':balanceUrl', data, success);
};
